package com.mucas;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Search {

	private String title  		= "";
	private String[] terms;
	private String type   		= null;
	private int level     		= 0;
	private int duration  		= 0;
	private boolean hours 		= false;
	
	Course currentCourse		= null;
	Pattern pNeedle;
	Matcher match1;
	Matcher match2;
	
	public Search() {
		//Constructor
	}
	
	public void setPattern(String pattern) {
		this.terms 	   = pattern.split(" ");
		String regex   = "";
		boolean first  = true;
		
		for( int i = 0; i < terms.length; i++) {
			if( terms[i] != null) {
				if( !first )
				{
					regex += "|";
					first  = true;
				}
				regex   += ".*" + terms[i] + ".*";
				first    = false;
			}
		}
		this.title = regex.toLowerCase();
		
	}
	
	public boolean doFilter(Course aCourse) {
		//if(aCourse.getHours() != this.hours)
		//	return false;
		//if(aCourse.getDuration() != this.duration && this.duration > 0)
		//	return false;
		//if(aCourse.getLevel() != this.level && this.level > 0)
		//	return false;
		//if(aCourse.getType() != this.type && this.type != null)
		//	return false;
		
		pNeedle = Pattern.compile(this.title);
		match1  = pNeedle.matcher(aCourse.getTitle().toLowerCase());
		//match2  = pNeedle.matcher(aCourse.getDesc().toLowerCase());
		if(!(match1.matches()) && this.title.compareTo("") != 0)
			return false;
		
		//return true;
		return true;
	}
	
	public String toString() {
		return this.title;
	}
}
